const RouteService = require('../services/routesService');

// 获取推荐路线列表
exports.getRecommendations = async (ctx) => {
  try {
    const { category, keyword } = ctx.query;
    const routes = RouteService.getRecommendations(category, keyword);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "获取成功",
      data: routes
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "获取失败",
      error: error.message
    };
  }
};

// 获取路线详情
exports.getRouteById = async (ctx) => {
  try {
    const { id } = ctx.params;
    const route = await RouteService.getRouteById(id);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "获取成功",
      data: route
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "获取失败",
      error: error.message
    };
  }
};

// 创建自定义旅游路线
exports.createRoute = async (ctx) => {
  try {
    const routeData = ctx.request.body;
    const newRoute = await RouteService.createRoute(routeData);
    ctx.status = 201;
    ctx.body = {
      code: 201,
      message: "创建成功",
      data: { id: newRoute.id }
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "创建失败",
      error: error.message
    };
  }
};

// 更新旅游路线
exports.updateRoute = async (ctx) => {
  try {
    const { id } = ctx.params;
    const updateData = ctx.request.body;
    await RouteService.updateRoute(id, updateData);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "更新成功"
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "更新失败",
      error: error.message
    };
  }
};

// 删除旅游路线
exports.deleteRoute = async (ctx) => {
  try {
    const { id } = ctx.params;
    await RouteService.deleteRoute(id);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "删除成功"
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "删除失败",
      error: error.message
    };
  }
};

// 用户自定义路线排序
exports.sortRouteSpots = async (ctx) => {
  try {
    const { route_id, spots_order } = ctx.request.body;
    await RouteService.sortRouteSpots(route_id, spots_order);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      message: "排序成功"
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: "排序失败",
      error: error.message
    };
  }
};
